#ifdef WX_PRECOMP //
#include "wx_pch.h"
#endif

#ifdef __BORLANDC__
#pragma hdrstop
#endif //__BORLANDC__

#include "setupdlg.h"
#include "sc/xml.h"


/**
* DlgAddChild constructor
*
* @param parent      parent window
* @param rcChildren  list of available children
*/
DlgAddChild::DlgAddChild(wxWindow* parent, const wxArrayString& rcChildren)
: DlgAddChildGui(parent)
{
	// Fill the available children list
	for (size_t i = 0; i < rcChildren.Count(); ++i)
	{
		m_choiceElement->Append(rcChildren[i]);
	}
	m_choiceElement->SetSelection(0);
}

/**
* Called when the user clicked on the OK button.
*  It must save the child name and close the dialog
*
* @param event
*/
void DlgAddChild::OnOK( wxCommandEvent& event )
{
    m_sChild = m_choiceElement->GetStringSelection();
    EndModal(wxID_OK);
}

/**
* DlgPreview constructor.
*
* @param parent Window parent
* @param rcCtx  Actual Context
*/
DlgPreview::DlgPreview(wxWindow* parent, const sc::Context& rcCtx)
: DlgPreviewGui(parent)
{
    for (size_t idx = 0; idx < rcCtx.GetSetupCount(); idx++)
    {
        const sc::SetupContext* pSetupCtx = rcCtx.GetSetupContext(idx);
        assert(pSetupCtx);

        // Add the XML file
        wxString sTmpFile = wxFileName::CreateTempFileName(_T("scxml_"));
        if ( !sTmpFile.empty() && !pSetupCtx->GetSetup().Save(sTmpFile) )
        {
            m_nbPreviews->AddPage( new PanelEditor(this, sTmpFile), pSetupCtx->GetXmlFile() );
        }

        // Add the XSLT result file
        sTmpFile = wxFileName::CreateTempFileName(_T("scxslt_"));
        if ( !sTmpFile.empty() && xml::XslTransform(pSetupCtx->GetXmlPath(), pSetupCtx->GetXslPath(), sTmpFile) )
        {
            m_nbPreviews->AddPage( new PanelEditor(this, sTmpFile), pSetupCtx->GetXsltResFile() );
        }
    }
}

/**
* PanelEditor event table
*/
BEGIN_EVENT_TABLE ( PanelEditor,  PanelEditorGui )
	EVT_SCI_MARGINCLICK( ID_EDITOR, PanelEditor::OnMarginClick )
END_EVENT_TABLE()

/**
* PanelEditor constructor.
*
* @param parent     Window parent
* @param sFilename  File name to edit
*/
PanelEditor::PanelEditor(wxWindow* parent, const wxString& sFilename)
: PanelEditorGui(parent)
{
    m_scintilla->SetLexer(wxSCI_LEX_XML);
    m_scintilla->StyleSetForeground(wxSCI_H_TAG, *wxBLUE);
    m_scintilla->StyleSetForeground(wxSCI_H_ATTRIBUTE, wxColour(0xc1, 0xa3, 0x3f));
    m_scintilla->StyleSetForeground(wxSCI_H_DOUBLESTRING, wxColour(0xff, 0x00, 0x00));
    m_scintilla->SetProperty( wxT( "fold.html" ), wxT( "1" ) );

    m_scintilla->LoadFile(sFilename);

    m_scintilla->SetReadOnly(true);
}

/**
* Called by framework when the user click on the editor margin
*
* @param event
*/
void PanelEditor::OnMarginClick ( wxScintillaEvent &event )
{
	if ( event.GetMargin() == 1 )
	{
		int lineClick  = m_scintilla->LineFromPosition ( event.GetPosition() );
		int levelClick = m_scintilla->GetFoldLevel ( lineClick );

		if ( (levelClick & wxSCI_FOLDLEVELHEADERFLAG) > 0 )
		{
			m_scintilla->ToggleFold ( lineClick );
		}
	}
}
